package org.fireflow2.designer.eclipse.parts.basic;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.fireflow.pdl.fpdl20.diagram.EndNodeShape;
import org.fireflow.pdl.fpdl20.diagram.basic.Circle;
import org.fireflow2.designer.eclipse.figures.EndNodeFigure;
import org.fireflow2.designer.eclipse.figures.StartNodeFigure;
import org.fireflow2.designer.eclipse.figures.widget.StartNode_EndNode_Anchor;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.EndNodeWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

public class BasicEndNodePart extends AbstractGraphicalEditPart implements
		NodeEditPart, PropertyChangeListener {

	@Override
	protected IFigure createFigure() {
		EndNodeWrapper wrapper = (EndNodeWrapper) this.getModel();
		
		IFigure figure = new EndNodeFigure(wrapper);

		return figure;
	}

	@Override
	protected void createEditPolicies() {

	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		
		
		EndNodeWrapper wrapper = (EndNodeWrapper) this.getModel();
		Rectangle rectangle = wrapper.calculateBounds();
		
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), rectangle);
		
		((EndNodeFigure)this.getFigure()).refreshFigure();
	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return new StartNode_EndNode_Anchor((Label)getFigure());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		// TODO Auto-generated method stub
		return new StartNode_EndNode_Anchor((Label)getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		// TODO Auto-generated method stub
		return new StartNode_EndNode_Anchor((Label)getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		// TODO Auto-generated method stub
		return new StartNode_EndNode_Anchor((Label)getFigure());
	}

	protected List getModelSourceConnections() {
//		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();
//		return modelWrapper.getLeavingLoops();
		return null;//TODO 2.0 待完善
	}

	protected List getModelTargetConnections() {
		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();
		return modelWrapper.getEnteringTransitions();
	}

	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		if (evt.getPropertyName().equals(NodeWrapper.LOCATION)) {
			this.refreshVisuals();
		} else if (evt.getPropertyName()
				.equals(NodeWrapper.ENTERING_TRANSITION))
			refreshTargetConnections();
		else if (evt.getPropertyName().equals(NodeWrapper.LEAVING_TRANSITION)||
				evt.getPropertyName().equals(NodeWrapper.LEAVING_LOOP))
			refreshSourceConnections();
	}

	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	}
}
